package com.yinhe.music.yhmusic.main.home;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RecommendAlbumAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceViewPagerFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.dialog.more.MoreDialog;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.model.HomeInfo;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.widget.SpaceDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;

public class NewSongFragment extends BaseServiceViewPagerFragment implements HomeContract.IHomeView,
        BaseQuickAdapter.OnItemChildClickListener{

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private int totalPage;
    private int curPage = 1;
    private int pageSize = 10;
    RecommendAlbumAdapter albumAdapter;

    HomePresenter mPresenter;

    public static NewSongFragment newInstance() {
        Bundle args = new Bundle();
        NewSongFragment fragment = new NewSongFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new HomePresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);

        albumAdapter = new RecommendAlbumAdapter();
        albumAdapter.setOnItemChildClickListener(this);
        // 上拉加载
        albumAdapter.setOnLoadMoreListener(() -> {
            if (curPage <= totalPage) {
                mPresenter.getRecommendNewSongs(curPage, pageSize);
            } else {
                albumAdapter.loadMoreEnd();
            }
        }, mRecyclerView);
        albumAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new SpaceDecoration(getActivity(), 0, 0, 10 ,0));
        mRecyclerView.setAdapter(albumAdapter);
    }

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();

        mPresenter.loadCacheDataForHome();
        mPresenter.getRecommendNewSongs(1, pageSize);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_list_new;
    }

    @Override
    public void setRecommend(HomeInfo homeInfo) {
        this.totalPage = homeInfo.getPageNum();
        List<Music> mSongList = homeInfo.getSongList();
        if (mSongList == null){
            return;
        }
        if (curPage == 1) {
            albumAdapter.setNewData(mSongList);
        }else {
            albumAdapter.addData(mSongList);
        }
        if (curPage >= totalPage) {
            albumAdapter.loadMoreEnd();
        }else {
            curPage++;
            albumAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_play:
                UmengEventUtils.clickRecommend(getActivity(), UmengEventUtils.SONG);
                mService.playPosition(albumAdapter.getData(), position);
                break;
            case R.id.playlist_more:
                MoreDialog.newInstance(albumAdapter.getData().get(position), 0, IConstants.NEW_MUSIC_MANAGER).
                        show(getFragmentManager(), "MoreDialog");
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this);
    }

    /**
     * 切换语系
     *
     * @param rxbusNullObject {@link RxbusNullObject}
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Home.CHANGE_LANG_TYPE)
    )
    public void onRxbusChangeLang(RxbusNullObject rxbusNullObject) {
        curPage = 1;
        mPresenter.getRecommendNewSongs(curPage, pageSize);
    }

    /**
     * 网络状态改变
     *
     * @param isConnected 连接状态
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Home.NETWORK_CONNECT)
    )
    public void onRxbusNetworkStatusDidChanged(Boolean isConnected) {
//        if (isConnected && mIsNetworkError) {
//            mIsNetworkError = false;
//            mPresenter.getRecommendList();
//        } else {
//            mIsNetworkError = true;
//        }
    }
}
